<!DOCTYPE html>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ~ Copyright © 2013-2016 The Nxt Core Developers.                            ~
  ~ Copyright © 2016-2017 Jelurida IP B.V.                                    ~
  ~                                                                           ~
  ~ See the LICENSE.txt file at the top-level directory of this distribution  ~
  ~ for licensing information.                                                ~
  ~                                                                           ~
  ~ Unless otherwise agreed in a custom licensing agreement with Jelurida B.V.,
  ~ no part of the Nxt software, including this file, may be copied, modified,~
  ~ propagated, or distributed except according to the terms contained in the ~
  ~ LICENSE.txt file.                                                         ~
  ~                                                                           ~
  ~ Removal or modification of this copyright notice is prohibited.           ~
  ~                                                                           ~
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->

<html>
    <head>
        <meta charset="UTF-8">
        <style type="text/css">
            body {background-color: #E0E0E0; color: #000; font-family: Tahoma; font-size: 10pt; margin: 0; padding: 10px;}
            table {border-collapse: collapse; width: 100%;}
            td {height: 20px; line-height: 20px; padding: 0;}
            .connected {background-image: url(); width: 20px;}
            .disabledAnnouncedAddress {background-image: url(); width: 20px;}
            .disabledDownloading {background-image: url(); width: 20px;}
            .disabledUploading {background-image: url(); width: 20px;}
            .disabledWeight {background-image: url(); width: 20px;}
            .disconnected {background-image: url(); width: 20px;}
            .enabledAnnouncedAddress {background-image: url(); width: 20px;}
            .enabledDownloading {background-image: url(); width: 20px;}
            .enabledUploading {background-image: url(); width: 20px;}
            .enabledWeight {background-image: url(); width: 20px;}
            .remove {background-image: url(); cursor: pointer; width: 20px;}
            .software {background-image: url(); width: 20px;}
            .text {cursor: default; padding: 0 10px 0 5px; white-space: nowrap;}
            .wellKnown {background-image: url(); width: 20px;}
        </style>
        <script type="text/javascript">
            var downloadingDeadlines = [], uploadingDeadlines = [];

            function addPeer(peer) {
                if (document.getElementById("peer" + peer.index) != null) {
                    document.getElementById("peers").deleteRow(document.getElementById("peer" + peer.index).rowIndex);
                }
                var peers = document.getElementById("peers"), row = peers.insertRow(peers.rows.length), element;
                row.id = "peer" + peer.index;
                element = row.insertCell(0);
                element.id = "state" + peer.index;
                if (peer.disconnected) {
                    element.className = "disconnected";
                    element.title = "Disconnected";
                } else {
                    element.className = "connected";
                    element.title = "Connected";
                }
                element = row.insertCell(1);
                element.className = "text";
                element.title = peer.address;
                element.innerHTML = peer.address;
                element = row.insertCell(2);
                element.id = "announcedAddressIcon" + peer.index;
                if (peer.wellKnown) {
                    element.className = "wellKnown";
                    element.title = "Well-known address";
                } else {
                    if (peer.announcedAddress.length > 1) {
                        element.className = "enabledAnnouncedAddress";
                    } else {
                        element.className = "disabledAnnouncedAddress";
                    }
                    element.title = "Announced address";
                }
                element = row.insertCell(3);
                element.id = "announcedAddress" + peer.index;
                element.className = "text";
                element.title = peer.announcedAddress;
                element.textContent = peer.announcedAddress;
                element = row.insertCell(4);
                element.id = "weightIcon" + peer.index;
                element.className = peer.weight > 0 ? "enabledWeight" : "disabledWeight";
                element.title = "Weight";
                element = row.insertCell(5);
                element.id = "weight" + peer.index;
                element.className = "text";
                element.innerHTML = formatWeight(peer.weight);
                element = row.insertCell(6);
                element.id = "downloading" + peer.index;
                element.className = "disabledDownloading";
                element.title = "Downloading";
                element = row.insertCell(7);
                element.id = "downloaded" + peer.index;
                element.className = "text";
                element.title = "Downloaded volume";
                element.innerHTML = formatVolume(peer.downloaded);
                element = row.insertCell(8);
                element.id = "uploading" + peer.index;
                element.className = "disabledUploading";
                element.title = "Uploading";
                element = row.insertCell(9);
                element.id = "uploaded" + peer.index;
                element.className = "text";
                element.title = "Uploaded volume";
                element.innerHTML = formatVolume(peer.uploaded);
                element = row.insertCell(10);
                element.className = "software";
                element.title = "Software";
                element = row.insertCell(11);
                element.className = "text";
                element.title = "Application (version)";
                element.textContent = peer.software;
                element = row.insertCell(12);
                element.id = "remove" + peer.index;
                element.className = "remove";
                element.title = peer.disconnected ? "Remove from the list" : "Disconnect and remove from the list";
                element.onclick = function() {
                    requestPeerRemoving(peer.index);
                };
                element = parent.document.getElementById("activePeersCounter");
                element.innerHTML = parseInt(element.innerHTML) + 1;
            }

            function addPeers(peers) {
                var i;
                for (i = 0; i < peers.length; i++) {
                    addPeer(peers[i]);
                }
            }

            function changePeer(peer) {try{
                var element;
                if (peer.connected) {
                    element = document.getElementById("state" + peer.index);
                    element.className = "connected";
                    element.title = "Connected";
                    document.getElementById("remove" + peer.index).title = "Disconnect and remove from the list";
                }
                if (peer.disconnected) {
                    element = document.getElementById("state" + peer.index);
                    element.className = "disconnected";
                    element.title = "Disconnected";
                    document.getElementById("remove" + peer.index).title = "Remove from the list";
                }
                if (peer.announcedAddress) {
                    element = document.getElementById("announcedAddress" + peer.index);
                    element.title = peer.announcedAddress;
                    element.textContent = peer.announcedAddress;
                    element = document.getElementById("announcedAddressIcon" + peer.index);
                    if (peer.wellKnown) {
                        element.className = "wellKnown";
                        element.title = "Well-known address";
                    } else {
                        if (peer.announcedAddress.length > 1) {
                            element.className = "enabledAnnouncedAddress";
                        } else {
                            element.className = "disabledAnnouncedAddress";
                        }
                        element.title = "Announced address";
                    }
                }
                if (peer.weight) {
                    element = document.getElementById("weightIcon" + peer.index);
                    element.className = peer.weight > 0 ? "enabledWeight" : "disabledWeight";
                    element = document.getElementById("weight" + peer.index);
                    element.innerHTML = formatWeight(peer.weight);
                }
                if (peer.downloaded > 0) {
                    downloadingDeadlines[peer.index] = (new Date()).getTime() + 1000;
                    document.getElementById("downloading" + peer.index).className = "enabledDownloading";
                    document.getElementById("downloaded" + peer.index).innerHTML = formatVolume(peer.downloaded);
                }
                if (peer.uploaded > 0) {
                    uploadingDeadlines[peer.index] = (new Date()).getTime() + 1000;
                    document.getElementById("uploading" + peer.index).className = "enabledUploading";
                    document.getElementById("uploaded" + peer.index).innerHTML = formatVolume(peer.uploaded);
                }}catch(e){}
            }

            function changePeers(peers) {
                var i;
                for (i = 0; i < peers.length; i++) {
                    changePeer(peers[i]);
                }
            }

            function formatVolume(volume) {
                var digits=[], formattedVolume = "", i;
                do {
                    digits[digits.length] = volume % 10;
                    volume = Math.floor(volume / 10);
                } while (volume > 0);
                for (i = 0; i < digits.length; i++) {
                    if (i > 0 && i % 3 == 0) {
                        formattedVolume = "'" + formattedVolume;
                    }
                    formattedVolume = digits[i] + formattedVolume;
                }
                return formattedVolume + " B";
            }

            function formatWeight(weight) {
                var digits=[], formattedWeight = "", i;
                do {
                    digits[digits.length] = weight % 10;
                    weight = Math.floor(weight / 10);
                } while (weight > 0);
                for (i = 0; i < digits.length; i++) {
                    if (i > 0 && i % 3 == 0) {
                        formattedWeight = "'" + formattedWeight;
                    }
                    formattedWeight = digits[i] + formattedWeight;
                }
                return formattedWeight;
            }

            function initialize() {
                setInterval(function() {
                    var i, time = (new Date()).getTime(), element;
                    for (i = 0; i < downloadingDeadlines.length; i++) {
                        if (downloadingDeadlines[i] > 0 && downloadingDeadlines[i] <= time) {
                            element = document.getElementById("downloading" + i);
                            if (element != null) {
                                element.className = "disabledDownloading";
                            }
                            downloadingDeadlines[i] = 0;
                        }
                    }
                    for (i = 0; i < uploadingDeadlines.length; i++) {
                        if (uploadingDeadlines[i] > 0 && uploadingDeadlines[i] <= time) {
                            element = document.getElementById("uploading" + i);
                            if (element != null) {
                                element.className = "disabledUploading";
                            }
                            uploadingDeadlines[i] = 0;
                        }
                    }
                }, 100);
            }

            function removePeer(peer) {try{
                document.getElementById("peers").deleteRow(document.getElementById("peer" + peer.index).rowIndex);
                var element = parent.document.getElementById("activePeersCounter");
                element.innerHTML = parseInt(element.innerHTML) - 1;}catch(e){}
            }

            function removePeers(peers) {
                var i;
                for (i = 0; i < peers.length; i++) {
                    removePeer(peers[i]);
                }
            }

            function requestPeerRemoving(index) {
                parent.sendRequest("removeActivePeer&peer=" + index);
            }
        </script>
    </head>
    <body onload="initialize();">
        <table id="peers"></table>
    </body>
</html>